package com.tiger.admin.http.feedback;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tiger.admin.Db;
import com.tiger.admin.SaveOperateLog;
import com.tiger.admin.dto.PaginationResult;
import com.tiger.admin.http.feedback.dto.PageDto;
import com.tiger.db.entity.FeedbackRecord;
import com.tiger.utils.ResponseUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/feedback")
public class FeedBackController {
    private final Db db;

    public FeedBackController(Db db) {
        this.db = db;
    }

    @PostMapping("page")
    public Object page(@RequestBody PageDto page, @RequestAttribute("memberId") long memberId) {
        PaginationResult<FeedbackRecord> result = new PaginationResult<>();

        LambdaQueryWrapper<FeedbackRecord> wrapper = new LambdaQueryWrapper<>();

        if(page.getStatus() != null) {
            wrapper.eq(FeedbackRecord::getStatus, page.getStatus());
        }


        long total = db.feedbackRecord.count(wrapper);
        if(total > 0) {
            result.setTotal(total);
            wrapper.orderByDesc(FeedbackRecord::getId);

            List<FeedbackRecord> rows = db.feedbackRecord.pagination(wrapper, page.getPageNo(), page.getPageSize());



            result.setRows(rows);
        }


        return ResponseUtil.successRes(result);
    }

    @SaveOperateLog("更新反馈")
    @PostMapping("update")
    public Object update(@RequestBody FeedbackRecord feedbackRecord) {
        db.feedbackRecord.update(new LambdaUpdateWrapper<FeedbackRecord>().set(FeedbackRecord::getStatus, feedbackRecord.getStatus()).eq(FeedbackRecord::getId, feedbackRecord.getId()));
        return ResponseUtil.successRes();
    }


    @SaveOperateLog("删除反馈")
    @PostMapping("delete/{id}")
    public Object del(@PathVariable Integer id, @RequestAttribute("memberId") long memberId) {
        FeedbackRecord row = db.feedbackRecord.getOne(new QueryWrapper<FeedbackRecord>().eq("id", id));
        if(row == null) return ResponseUtil.successRes();
        db.feedbackRecord.removeById(id);
        return ResponseUtil.successRes();
    }
}
